//
//  HYBDPanFileListCell.m
//  XPlayer
//
//  Created by 张水 on 2023/9/15.
//

#import "HYBDPanFileListCell.h"
#import <HYUtls/CommUtls+Time.h>
#import "NSArray+SafeKit.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface HYBDPanFileListCell ()
///文件类型 && 文件大小
@property (nonatomic, strong) UILabel *fileTypeLabel;

@property (nonatomic, strong) HYBDPanFileModelList *fileMeta;

@end

@implementation HYBDPanFileListCell

- (void)configUI
{
    [super configUI];
    [self.backView addSubview:self.coverView];
    [self.coverView addSubview:self.iconView];
    [self.backView addSubview:self.titleLabel];
    [self.backView addSubview:self.fileTypeLabel];
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.backView);
        make.width.mas_equalTo(kCoverWidth);
    }];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.coverView);
        make.width.height.mas_equalTo(44);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.coverView.mas_right).offset(10);
        make.top.equalTo(self.coverView.mas_top).offset(5);
        make.right.equalTo(self.backView).offset(-5);
    }];
    [self.fileTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.bottom.equalTo(self.coverView.mas_bottom).offset(-5);
        make.right.equalTo(self.backView).offset(-5);
    }];
    self.setBtn.hidden = YES;
}

- (void)bindViewModel:(HYBDPanFileModelList *)fileMeta
{
    _fileMeta = nil;
    _fileMeta = fileMeta;
    NSMutableArray *array = [NSMutableArray arrayWithArray:[fileMeta.server_filename componentsSeparatedByString:@"."]];
    NSString *tmpfiletype = @"";
    if (array.count > 1){
        tmpfiletype = [NSString stringWithFormat:@"%@", array.hy_lastObject];
        [array removeObject:array.hy_lastObject];
    }
    self.titleLabel.text = fileMeta.server_filename;
    
    NSString *tmpduration;
    NSString *tmpSize;
    UIImage *image;
    switch (fileMeta.category) {
        case BDPanFileTypeVideo://视频
            {
                image = [UIImage imageNamed:@"icon_video"];
                if (fileMeta.duration.length != 0) {
                    tmpduration = [CommUtls timeToHMS:[fileMeta.duration floatValue]];
                }
            }
            break;
        case BDPanFileTypeAudio://音频
        {
            image = [UIImage imageNamed:@"icon_file_music"];
            if (fileMeta.duration.length != 0) {
                tmpduration = [CommUtls timeToHMS:[fileMeta.duration floatValue]];
            }
        }
            break;
        case BDPanFileTypePhoto:// 图片
        {
            image = [UIImage imageNamed:@"icon_picture"];
        }
            break;
        case BDPanFileTypeDocument:// 文档
        {
            image = [UIImage imageNamed:@"icon_file_text"];
        }
            break;
        case BDPanFileTypeIAPP://应用
        {
            image = [UIImage imageNamed:@"icon_file_other"];
            if ([self compareString:tmpfiletype With:@"apk"] ||
                [self compareString:tmpfiletype With:@"ipa"]) {
                image = [UIImage imageNamed:@"app"];
            }
            if ([self compareString:tmpfiletype With:@"exe"]) {
                image = [UIImage imageNamed:@"icon_exe"];
            }
        }
            break;
        case BDPanFileTypeOther:
        {
            if (tmpfiletype.length == 0) {
                image = [UIImage imageNamed:@"list_folder_mode"];
            }else{
                image = [UIImage imageNamed:@"icon_file_zip"];
            }
        }
            break;
        case BDPanFileTypeSeed:
        {
            image = [UIImage imageNamed:@"icon_file_other"];
        }
            break;
        default:
            break;
    }
    
    if(fileMeta.category == BDPanFileTypeVideo || fileMeta.category == BDPanFileTypePhoto){
        self.iconView.image = image;
        //经过查看返回数据结果来看URL3 尺寸是最大 最清楚的 依次为3 4 2 1 icon
        NSURL *iamgeURL;
        @weakify(self);
        if (fileMeta.thumbs.url3.length != 0){
            iamgeURL = [NSURL URLWithString:fileMeta.thumbs.url3];
        }else if (fileMeta.thumbs.url4.length != 0){
            iamgeURL = [NSURL URLWithString:fileMeta.thumbs.url4];
        }else if (fileMeta.thumbs.url2.length != 0) {
            iamgeURL = [NSURL URLWithString:fileMeta.thumbs.url2];
        }else if (fileMeta.thumbs.url1.length != 0){
            iamgeURL = [NSURL URLWithString:fileMeta.thumbs.url1];
        }else if (fileMeta.thumbs.icon.length != 0) {
            iamgeURL = [NSURL URLWithString:fileMeta.thumbs.icon];
        }
        [self.coverView  sd_setImageWithURL:iamgeURL placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            @strongify(self);
            if (error) {
                self.coverView.hidden = YES;
                self.iconView.hidden = NO;
            }else{
                self.iconView.hidden = YES;
                fileMeta.videoImage = image;
            }
        }];
    }else{
        self.iconView.hidden = YES;
        self.coverView.image = image;
    }

    tmpSize = [self getSystemSize:[fileMeta.size floatValue]];
    NSString *size = @"";
    NSString *duration = @"";
    if(tmpSize.length != 0) {
        size = [NSString stringWithFormat:@"%@",tmpSize];
    }
    
    if(tmpduration.length != 0){
        duration = [NSString stringWithFormat:@"| %@",tmpduration];
    }
    self.fileTypeLabel.text = [NSString stringWithFormat:@"%@%@", size,duration];
    
}
- (NSString *)getSystemSize:(CGFloat)size
{
    NSString *bytes;
    if(size == 0){
        return @"";
    }
    if (size > 1024 * 1024 * 1024) {
        bytes = [NSString stringWithFormat:@"%0.2fG",size/1024/1024/1024];
    } else if (size < 1024 * 1024 * 1024 && size>= 1024 * 1024) {
        bytes = [NSString stringWithFormat:@"%0.2fM",size/1024/1024];
    } else if (size < 1024 * 1024 && size>= 1024) {
        bytes = [NSString stringWithFormat:@"%.2fK",size/1024];
    } else {
        bytes = [NSString stringWithFormat:@"%.2fB",size];
    }
    return bytes;
}

- (BOOL)compareString:(NSString *)string With:(NSString *)key
{
    BOOL result = NO;
    if ([string caseInsensitiveCompare:key] == NSOrderedSame) {
        result = YES;
    }
    return result;
}

#pragma mark  - loadLazy

- (UILabel *)fileTypeLabel
{
    if(!_fileTypeLabel){
        _fileTypeLabel = [[UILabel alloc]init];
        _fileTypeLabel.font = FONT_SIZE(14);
        _fileTypeLabel.textColor = UIColor.systemGrayColor;
    }
    return _fileTypeLabel;
}


@end
